//
//  BackgroundLayer.m
//  LearningCocos
//
//  Created by hansangyong on 2014. 2. 14..
//  Copyright (c) 2014년 ainhan. All rights reserved.
//

#import "BackgroundLayer.h"

static const int kScrollSpeed = 12;

@implementation BackgroundLayer

- (void)update:(ccTime)delta {
    if ([bgArray count] > 0) {
        for (int i = 0; i < [bgArray count]; i++) {
            
            CGPoint bgPos = ((CCSprite *)[bgArray objectAtIndex:i]).position;
            
            bgPos.x -= kScrollSpeed;
            
            if (bgPos.x < -(((CCSprite *)[bgArray objectAtIndex:i]).contentSize.width)) {
                float margin = (-1 * bgPos.x) - (((CCSprite *)[bgArray objectAtIndex:i]).contentSize.width);
                bgPos.x = _screenSize.width - margin;
            }
            
            ((CCSprite *)[bgArray objectAtIndex:i]).position = bgPos;
        }
    }
    
    
/*
    CGPoint bg1Pos = _bg1.position;
    CGPoint bg2Pos = _bg2.position;
    bg1Pos.x -= kScrollSpeed;
    bg2Pos.x -= kScrollSpeed;
    
    if (bg1Pos.x < -(_bg1.contentSize.width)) {
        bg1Pos.x = _bg1.contentSize.width;
    }

    if (bg2Pos.x < -(_bg2.contentSize.width)) {
        bg2Pos.x = _bg2.contentSize.width;
    }
    
    _bg1.position = bg1Pos;
    _bg2.position = bg2Pos;
*/
}

-(CCSprite*)blankSpriteWithSize:(CGSize)size
{
    CCSprite *sprite = [CCSprite node];
    GLubyte *buffer = malloc(sizeof(GLubyte)*4);
    for (int i=0;i<4;i++) {buffer[i]=255;}
    CCTexture2D *tex = [[CCTexture2D alloc] initWithData:buffer pixelFormat:kCCTexture2DPixelFormat_RGB5A1 pixelsWide:1 pixelsHigh:1 contentSize:size];
    [sprite setTexture:tex];
    [sprite setTextureRect:CGRectMake(0, 0, size.width, size.height)];
    free(buffer);
    return sprite;
}

- (id)init {
    self = [super init];
    if(self == nil)
        return nil;
    
    _screenSize = [CCDirector sharedDirector].winSize;
    
    bgArray = [[NSMutableArray alloc] init];
    float underSize = 0.0f;
    BOOL bgOrder = YES;
    CCSprite * _bg = nil;
    float spriteWidth = [[CCSprite spriteWithFile:@"pense.png"] contentSize].width;
    
    while (underSize < _screenSize.width + spriteWidth) {
/*
        if (bgOrder) {
            _bg = [CCSprite spriteWithFile:@"handDown.png"];
            _bg.anchorPoint = CGPointMake(0.0f, 0.5f);
        } else {
            _bg = [CCSprite spriteWithFile:@"jumpDown.png"];
            _bg.anchorPoint = CGPointMake(0.0f, 0.5f);
        }
*/
        _bg = [CCSprite spriteWithFile:@"pense.png"];
        _bg.anchorPoint = CGPointMake(0.0f, 0.5f);

        _bg.position = CGPointMake(underSize, _screenSize.height * 0.5f);
        [bgArray addObject:_bg];
        [self addChild:_bg];
        underSize += _bg.contentSize.width;
        bgOrder = !bgOrder;
    }
    
//    int lastBuildingWidth = 100;
//    for (int buildingIndex = 1; buildingIndex <= BUILDING_VARIETY; buildingIndex++) {
//        CCSprite *building = [self blankSpriteWithSize:CGSizeMake(100.0f, 200.0f)];
//    }
    
    
/*
    CCSprite *backgroundImage;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        backgroundImage = [CCSprite spriteWithFile:@"background.png"];
    } else {
        backgroundImage = [CCSprite spriteWithFile:@"backgroundiPhone.png"];
    }
    
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    [backgroundImage setPosition:CGPointMake(screenSize.width / 2, screenSize.height / 2)];
    
    [self addChild:backgroundImage z:0 tag:0];
    
    return self;
*/
    [self scheduleUpdate];
    
    return self;
}

@end
